package com.greatwqs.wmall.user.provider.manager.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.greatwqs.wmall.common.module.exception.AppException;
import com.greatwqs.wmall.common.module.exception.ErrorCode;
import com.greatwqs.wmall.common.module.model.LoginUser;
import com.greatwqs.wmall.common.module.util.JsonUtil;
import com.greatwqs.wmall.user.client.enums.UserStatusType;
import com.greatwqs.wmall.user.provider.manager.UserManager;
import com.greatwqs.wmall.user.provider.mapper.UserMapper;
import com.greatwqs.wmall.user.provider.model.po.User;

import lombok.extern.slf4j.Slf4j;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * User 通用功能实现
 *
 * @author wang-qingsong
 */
@Slf4j
@Component
public class UserManagerImpl implements UserManager {

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private UserMapper userMapper;

    // @PostConstruct
    public void testPageHelper() {
        List<User> userAll = new ArrayList<>();
        for (int page = 1; page <= 15; page++) {
            Page<User> userQuery = this.getPageByUserStatus(UserStatusType.CREATED, page, 10);
            log.warn("PageHelper page: " + page + ", userQuery: " + JsonUtil.toJson(userQuery));
            userAll.addAll(userQuery);
        }
        log.warn("PageHelper userAll: " + JsonUtil.toJson(userAll));
    }

    // @PostConstruct
    public void addUsers() {
        for (int index = 100; index <= 200; index++) {
            final String userId = "user-id-" + index;
            User user = new User();
            user.setUserId(null);
            user.setLoginId(userId + "@qq.com");
            user.setUserPwd(userId + "-pwd");
            user.setUserName(userId + "-username");
            user.setPhoneNo("15802880040");
            user.setEmailAdd(userId + "@qq.com");
            user.setUserImg("/" + userId + ".jpg");
            user.setUserStatus(UserStatusType.CREATED.getValue());
            user.setValid(true);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            userMapper.insert(user);

            log.warn("inserted user: " + user.getUserId());
        }
    }

    @Override
    public User getPo(Long userId) {
        return userMapper.selectByUserId(userId);
    }

    @Override
    public User getPoNotNull(Long userId) {
        User po = getPo(userId);
        if (po == null) {
            log.warn("User not found, userId: " + userId);
            throw new AppException(ErrorCode.USER_NOT_FOUND);
        }
        return po;
    }

    @Override
    public User getPoByLoginId(String loginId) {
        return userMapper.selectByLoginId(loginId);
    }

    @Override
    public LoginUser getLoginUserNotNull(Long userId) {
        return modelMapper.map(getPoNotNull(userId), LoginUser.class);
    }

    @Override
    public Page<User> getPageByUserStatus(UserStatusType userStatus, int page, int pageSize) {
        return PageHelper.startPage(page, pageSize, true)
                .setOrderBy("seq desc")
                .doSelectPage(() -> userMapper.selectByUserStatus(userStatus.getValue()));
    }

    @Override
    public PageInfo<User> getPageInfoByUserStatus(UserStatusType userStatus, int page, int pageSize) {
        return getPageByUserStatus(userStatus, page, pageSize).toPageInfo();
    }
}
